<?php

/* Created by: Adam Malevski
 * Date: 2011-12-07
 * Class works with categories
 */

class Category {
    var $link, $id, $data;
    
    function __construct($id = NULL, $name = NULL) {
        require_once 'db.class.php';
        include("mysql.config.php");
        $this->link = new DataBase($db_address, $db_username, $db_password, $db_db);
        if (isset ($name)) {
            $this->link->insert("categories", array($name), "name");
            $lid = mysql_insert_id();
            $this->link->select("categories", "*", "`id`='$lid'");
            $this->id  = $lid;
            $this->data = $this->link->getResult();
        }
        else {
            $this->link->select("categories", "*", "`id`='$id'");
            $this->id  = $id;
            $this->data = $this->link->getResult();
        }
    }
    
    /*
     * Function gets category ID
     * @return - returns category id
     * @rtype - integer
     */
    function getId() {
        return $this->id;
    }
    
    /*
     * Function gets category name
     * @return - returns category name
     * @rtype - string
     */
    function getName() {
        return $this->data['name'];
    }
    
    /*
     * Function sets category name
     * @param - category name
     * @ptype - string
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function setName($name) {
        $res = $this->link->query("UPDATE `categories` SET `name` = '$name' WHERE `id` = '$this->id'");
        if ($res)
            return 1;
        else
            return 0;
    }
}

?>
